package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Notice;
import cn.wolfcode.crm.mapper.EmployeeMapper;
import cn.wolfcode.crm.mapper.NoticeMapper;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.INoticeService;
import cn.wolfcode.crm.util.EmployeeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoticeServiceImpl implements INoticeService {

    @Autowired
    private NoticeMapper noticeMapper;


    @Override
    public void save(Notice notice) {
        noticeMapper.insert(notice);
    }

    @Override
    public void delete(Long id) {
        noticeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Notice notice) {
        noticeMapper.updateByPrimaryKey(notice);
    }

    @Override
    public Notice get(Long id) {
        return noticeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Notice> listAll() {
        return noticeMapper.selectAll();
    }

    @Override
    public PageInfo<Notice> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize()); //对下一句sql进行自动分页
        List<Notice> notices = noticeMapper.selectForList(qo); //里面不需要自己写limit
        Employee employee = EmployeeUtil.getEmployee();
        List<Notice> noticesm = noticeMapper.selectByEmployeeId(employee.getId());
        for (Notice notice : notices) {
            //notice.setStatus(false);
            if (noticesm != null/* && noticesm.contains(notice)*/) {
                for (Notice notice1 : noticesm) {
                    if (notice.getId().equals(notice1.getId())) {
                        notice.setStatus(true);
                    }
                }
            }
        }

        return new PageInfo<>(notices);
    }



    @Override
    public List<Notice> selectByEmployeeId(Long id) {
        return noticeMapper.selectByEmployeeId(id);
    }

    @Override
    public void insertByEmployeeId(Long employeeId, Long id) {
        noticeMapper.insertByEmployeeId(employeeId,id);
    }

    @Override
    public Notice selectByNoticeId(Long employeeId, Long id) {
        return noticeMapper.selectByNoticeId(employeeId,id);
    }
}
